// pages/repair/repair.js
import Toast from '@vant/weapp/toast/toast';
const app= getApp()
const repairTypeOption = [
  {
    text: '房屋建筑',
    value: '房屋建筑',
    children: [
      { text: '墙面脱落/破损/渗水/漏水（教学楼及宿舍内）', value: '墙面脱落/破损/渗水/漏水（教学楼及宿舍内）' },
      { text: '卫生间/沐浴间集成吊顶漏水', value: '卫生间/沐浴间集成吊顶漏水' },
      { text: '墙面需要刮白', value: '墙面需要刮白' },
      { text: '公共区域路面破损', value: '公共区域路面破损' },
      { text: '其他问题', value: '其他问题' },
    ],
  },
  {
    text: '疏通类别',
    value: '疏通类别',
    children: [
      { text: '卫生间堵塞', value: '卫生间堵塞' },
      { text: '公共区域管道堵塞', value: '公共区域管道堵塞' },
      { text: '洗手池堵塞', value: '洗手池堵塞' },
      { text: '阳台地漏堵塞', value: '阳台地漏堵塞' },
      { text: '拖把池堵塞', value: '拖把池堵塞' },
    ],
  },
  {
    text: '电器类别',
    value: '电器类别',
    children: [
      { text: '空调', value: '空调' },
      { text: '开水机', value: '开水机' },
      { text: '洗衣机', value: '洗衣机' },
      { text: '烘干机', value: '烘干机' },
    ],
  },
  {
    text: '电工类别',
    value: '电工类别',
    children: [
      { text: '室内灯问题', value: '室内灯问题' },
      { text: '沐浴间的灯', value: '沐浴间的灯' },
      { text: '卫生间的灯', value: '卫生间的灯' },
      { text: '公共楼道灯', value: '公共楼道灯' },
      { text: '墙上插座', value: '墙上插座' },
      { text: '墙上面板开关', value: '墙上面板开关' },
      { text: '排气扇', value: '排气扇' },
    ],
  },
  {
    text: '水工类别',
    value: '水工类别',
    children: [
      { text: '洗手水池问题', value: '洗手水池问题' },
      { text: '宿舍内热水表问题', value: '宿舍内热水表问题' },
      { text: '水龙头问题', value: '水龙头问题' },
      { text: '卫生间内脚踏阀及感应问题', value: '卫生间内脚踏阀及感应问题' },
      { text: '沐浴间花洒问题', value: '沐浴间花洒问题' },
      { text: '洗手盆破损/脱落掉下', value: '洗手盆破损/脱落掉下' },
    ],
  },
  {
    text: '木工类别',
    value: '木工类别',
    children: [
      { text: '金属门/门栓/锁/问题', value: '金属门/门栓/锁/问题' },
      { text: '玻璃门/窗/锁/把手问题', value: '玻璃门/窗/锁/把手问题' },
      { text: '床、桌、凳、椅、柜子问题', value: '床、桌、凳、椅、柜子问题' },
      { text: '宿舍内蚊帐架轨道问题', value: '宿舍内蚊帐架轨道问题' },
      { text: '阳台晾衣杆问题', value: '阳台晾衣杆问题' }
    ],
  },
  {
    text: '网络故障类别',
    value: '网络故障类别',
    children: [
      { text: '教职工公寓网络故障', value: '教职工公寓网络故障' },
      { text: '公共区域网络故障', value: '公共区域网络故障' },
      { text: '办公区域网络故障', value: '办公区域网络故障' }
    ],
  },
];
const options = [
      {
          value: "香樟苑区域",
          text: "香樟苑区域",
          children: [],
      },
      {
          value: "上茶苑区域",
          text: "上茶苑区域",
          children: [],
      },
      {
          value: "下茶苑区域",
          text: "下茶苑区域",
          children: [],
      },
      {
          value: "长芗苑区域",
          text: "长芗苑区域",
          children: [],
      },
      {
          value: "芭蕉苑区域",
          text: "芭蕉苑区域",
          children: [],
      },
  ];
  for (let index = 1; index < 3; index++) {
        options[0].children.push({
            value: `宿${index}斋`,
            text: `宿${index}斋`,
        });
    }
    for (let index = 3; index < 6; index++) {
        options[1].children.push({
            value: `宿${index}斋`,
            text: `宿${index}斋`,
        });
    }
    for (let index = 6; index < 10; index++) {
        options[2].children.push({
            value: `宿${index}斋`,
            text: `宿${index}斋`,
        });
    }
    for (let index = 10; index < 14; index++) {
        options[3].children.push({
            value: `宿${index}斋`,
            text: `宿${index}斋`,
        });
    }
    for (let index = 14; index < 16; index++) {
        options[4].children.push({
            value: `宿14斋${index == 14 ? "A" : "B"}区`,
            text: `宿14斋${index == 14 ? "A" : "B"}区`,
        });
    }
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:app.globalData.userInfo,
    repairInfo:{
      userID:app.globalData.userInfo.id,
      description:"",
      repairs:"",
      address:app.globalData.userInfo.dormitory,
      repairImage:[],
      type:"",
      area:"",
      makeTime:""
    },
    textareaSize:{
      minHeight: 60
    },
    fileList:[],
    popupShow:false,
    popupOption:options,
    cascaderTitle:"",
    repair:""
  },
  afterRead(event) {
    let toast = Toast.loading("上传中")
    let that = this;
    const { file } = event.detail;
    const { fileList = [],repairInfo} = that.data;
    file.forEach((item,i)=>{
      wx.uploadFile({
        url: app.globalData.serviceUrl+'/upload/uploadImage',
        filePath: item.url,
        name: 'image',
        success:(res)=>{
          let image = JSON.parse(res.data).data.replace('[',"").replace(']',"");
          fileList.push({ ...item,url:item.url,deletable: true})
          repairInfo.repairImage.push(image)
          that.setData({
            'repairInfo.repairImage':repairInfo.repairImage,
            fileList:fileList
          })
          if(file.length-1==i){
            toast.clear();
          }
        },
      });
    })
  },
  submitRepair(e){
    let that = this;
    let {userID,address,area,description} = e.detail.value;
    if(area==''||address==''){
      Toast("报修区域或详细地址不能为空")
      return
    }
    if(that.data.repairInfo.type==""||that.data.repairInfo.repairs==""||description==""){
      Toast("报修事项或故障描述不能为空")
      return
    }
    wx.request({
      url: app.globalData.serviceUrl+'/repair/submitRepair',
      method:"POST",
      data:{
        userID:userID,
        userType:app.globalData.storage.userType,
        address:address,
        area:area,
        repairs:that.data.repairInfo.repairs,
        repairImage:that.data.repairInfo.repairImage.join(","),
        description:description,
        type:that.data.repairInfo.type
      },
      success:(res)=>{
        if(res.data.code==1){
          wx.switchTab({
            url: '/pages/index/index',
            success:()=>{
              Toast(res.data.msg)
            }
          })
        }
        Toast(res.data.msg)
      }
    })
  },
  changePopupShow(e){
    this.setData({
      cascaderTitle:e.currentTarget.dataset.title,
      popupOption:e.currentTarget.dataset.title=='请选择报修事项'?repairTypeOption:options,
      popupShow:!this.data.popupShow
    })
  },
  onChange(e){
    if(e.detail.tabIndex==0){
      if(this.data.popupOption==repairTypeOption){
        this.setData({
          'repairInfo.type':e.detail.value
        })
        return
      }
    }
  },
  onFinish(e){
    const { selectedOptions, value } = e.detail;
    const repair = selectedOptions
        .map((option) => option.text || option.name)
        .join('/');
    if(this.data.popupOption==repairTypeOption){
      this.setData({
        repair:repair,
        'repairInfo.repairs':value
      })
      return
    }
    this.setData({
      'repairInfo.area':repair
    })
  },
  delImg(e){
    this.setData({
      fileList:this.data.fileList.filter((item,i)=>i!==e.detail.index),
      'repairInfo.repairImage':this.data.repairInfo.repairImage.filter((item,i)=>i!==e.detail.index)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})